package ciyuan.wrap;

import ciyuan.QQLogin;
import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.event.LoginEvent;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.event.EventHandler;

public class BCMain extends Plugin implements Listener {

    @Override
    public void onEnable() {
        Wrapper.INSTANCE = new BCWrap(this);
        BungeeCord.getInstance().getPluginManager().registerListener(this, this);
        QQLogin.onEnable();
    }

    @EventHandler
    public void onLogin(ServerConnectEvent event) {
        event.getPlayer().sendMessage(QQLogin.config.SuccessMsg);
    }

    @EventHandler(priority = 9)
    public void onPreJoin(LoginEvent event) {
        String address = event.getConnection().getSocketAddress().toString().replace("/", "");
        address = address.substring(0, address.indexOf(":"));
        String kickMsg = QQLogin.onJoin(event.getConnection().getName(), address);
        if (kickMsg != null && !event.isCancelled()) {
            event.setCancelReason(kickMsg);
            event.setCancelled(true);
        }
    }

}
